home *** CD-ROM | disk | FTP | other *** search
/ Resource for Source: C/C++ / Resource for Source - C-C++.iso / misc_src / cslib16b / demo / address / csadd.cpp next >
Encoding:
C/C++ Source or Header  |  1995-11-01  |  13.2 KB  |  576 lines

  1. /***********************************************************************
  2.  
  3.  
  4.  
  5.                An DEMO address database
  6.                 build with
  7.              the CS-Libraries.
  8.  
  9.                Version 1.1.a.
  10.  
  11.  
  12.  
  13.                                            Copyright(c) 1994,1995 
  14.                                                            Combis 
  15.                                                   The Netherlands 
  16. ***********************************************************************/
  17.  
  18.  
  19. #include "ctype.h"
  20. #include "csa.h"
  21. #include "csdb.h"
  22. #include "csaddio.h"
  23.  
  24. //  Menu Options
  25.  
  26. #define mREINDEX    700
  27. #define mCREATE_DB    701
  28. #define mEXPORT_ASCII    702
  29. #define mEXPORT_DBASE    703
  30. #define mIMPORT_ASCII    704
  31.  
  32. #define mUNSORT     501
  33. #define mSORT_NAME    502
  34. #define mSORT_CITY    503
  35. #define mSORT_BIRTHDAY    504
  36. #define mSORT_RELATION    505
  37.  
  38.  
  39.  
  40.  
  41.  
  42. //////////////////////////// A pop up window for messages /////////////////////
  43. WINDOW w_mess;
  44.  
  45. void message_window_clear(void) { w_mess.remove(); }
  46.  
  47.  
  48. void message_window(char *s)
  49. {
  50.      w_mess.height(10);
  51.      w_mess.width(40);
  52.      w_mess.head(" ** Message ** ");
  53.      w_mess.border(W_BORDER_DOUBLE);
  54.      w_mess.activate();
  55.      gotoxy(max(1,(40-2-(int)strlen(s))/2),4);
  56.      cputs(s);
  57. }
  58.  
  59.  
  60. ///////////////////////////////////////////////////////////////////////////
  61.  
  62.  
  63. class NAM: public NAM_foundation
  64. {
  65.  protected:
  66.  
  67.    int choice;
  68.  
  69.    MENU   m1,m2,m3,m4,m5;
  70.    PANEL  panel;
  71.  
  72.  
  73.  public:
  74.  
  75.  
  76.    void insert(void);
  77.    void def_menu(void);
  78.    int    use_menu(void)    { return (m1.choose(choice)==ENTER) ? choice: -1; }
  79.    int    use_menu(int n) { m1.find_path(n); return use_menu(); }
  80.  
  81.    void edit(void);
  82.    void display(void)    { panel.display(); }
  83.    void recreate(void)    { panel.recreate(); }
  84.    void create(void);
  85.  
  86. };
  87.  
  88.  
  89. /////////////////////////////////////////////////////////////////////////
  90. void NAM::insert(void)
  91. {
  92.  
  93.   append_blank();
  94.  
  95.   _update.now();
  96.  
  97.   recreate();
  98.   display();
  99.   panel.read();
  100.  
  101. }
  102.  
  103. /////////////////////////////////////////////////////////////////////////
  104. void NAM::create(void)
  105. {
  106.  
  107.   panel.remove();
  108.  
  109.   panel.set_dim(3,-1,18,65);               // Size and Position of panel
  110.   panel.head(" Address DataBase ");            // Heading
  111.   panel.border(BORDER_DOUBLE);               // Type of border
  112.   panel.activate();                   // Make panel-window visible
  113.  
  114.   gotoyx(3,10); cprintf("Name: ");
  115.   panel.add_field(4,10,25,NAME_LENGTH,rec._name);
  116.  
  117.   gotoyx(3,40); cprintf("City: ");
  118.   panel.add_field(4,40,15,CITY_LENGTH,rec._city);
  119.  
  120.   gotoyx(6,10); cprintf("Address: ");
  121.   panel.add_field(7,10,25,ADRE_LENGTH,rec._adre);
  122.  
  123.   gotoyx(6,40); cprintf("Telephone: ");
  124.   panel.add_field(7,40,15,TEL_LENGTH,rec._tel);
  125.  
  126.   gotoyx(9,10); cprintf("Zip code: ");
  127.   panel.add_field(10,10,ZIP_LENGTH,rec._zip);
  128.  
  129.   gotoyx(9,40); cprintf("Country: ");
  130.   panel.add_field(10,40,15,COUNT_LENGTH,rec._count);
  131.  
  132.  
  133.   gotoyx(1,43); cprintf("Updated: ");
  134.   panel.add_field(1,53,_update);
  135.   panel.protect(DISPLAY);
  136.  
  137.   gotoyx(12,10); cprintf("Birthday: ");
  138.   panel.add_field(13,10,_birth);
  139.  
  140.   gotoyx(12,40); cprintf("Relation: ");
  141.   panel.add_field(13,40,RELATION_LENGTH,rec._relation );
  142.  
  143.   gotoyx(15,10); cprintf("Info: ");
  144.   panel.add_field(16,10,50,INFO_LENGTH,rec._info );
  145.  
  146.   panel.exit_key(CTRL_END);
  147.   panel.escape_off();
  148.   panel.display();
  149.  
  150. }
  151.  
  152. /////////////////////////////////////////////////////////////////////////
  153. void NAM::edit(void)
  154. {
  155.  
  156.   WINDOW help;
  157.   help.set_dim( 21,-1,3,40);
  158.   help.border(W_BORDER_DOUBLE);
  159.   help.activate();
  160.   cprintf("    Use CTRL_END to exit editing.");
  161.  
  162.   panel.read();
  163.  
  164.   if(panel.changed())
  165.   {
  166.     dirty=TRUE;
  167.     _update.now();
  168.   }
  169.  
  170. }
  171. /////////////////////////////////////////////////////////////////////////
  172.  
  173. void NAM::def_menu(void)
  174. {
  175.  
  176.  
  177. /////////////////////////// Define the options for menu 1  //////////////
  178.  
  179.  m1.add_option(" e~Xit "        ,ALT_X );
  180.  m1.add_option(" ~Insert "      ,ALT_I );
  181.  m1.add_option(" ~Delete "      ,ALT_D );
  182.  m1.add_option(" ~Edit "        ,ALT_E );
  183.  m1.add_option(" ~Sort order "  ,ALT_S );
  184.  m1.add_option(" ~Output "      ,ALT_O );
  185.  m1.add_option(" ~Utilities "   ,ALT_U );
  186.  m1.add_option(" Se~tup "       ,ALT_T );
  187.  m1.add_option("      ~Help=F1 ",F1 );
  188.  
  189.  
  190. /////////// Define the options for the output submenu ///////////////////////
  191.  
  192.  m2.add_option(" P~ages " , 601 );
  193.  m2.add_option(" P~rinten ",602 );
  194.  
  195.  
  196. /////////// Define the options for the utility submenu //////////////////////
  197.  
  198.  m3.add_option(" ~Pack  "             ,ALT_P );
  199.  m3.add_option(" ~Rebuild Indexes "   ,mREINDEX);
  200.  m3.add_option(" ~Create new database",mCREATE_DB);
  201.  m3.add_option(" ~Export to ASCII "   ,mEXPORT_ASCII);
  202.  m3.add_option(" Export to d~BASE "   ,mEXPORT_DBASE);
  203.  m3.add_option(" ~Import from ASCII " ,mIMPORT_ASCII);
  204.  
  205. /////////// Define the options for the setup submenu ////////////////////////
  206.  
  207.  
  208.  m4.add_option(" Directories ",603 );
  209.  m4.add_option(" Colors "     ,604 );
  210.  m4.add_option(" Save Setup"  ,605 );
  211.  
  212. /////////// Define the options for the 'sort' submenu ///////////////////////
  213.  
  214.  
  215.  m5.add_option(" ~Unsorted "          ,mUNSORT);
  216.  m5.add_option(" Sorted by ~Name "    ,mSORT_NAME);
  217.  m5.add_option(" Sorted by ~City "    ,mSORT_CITY);
  218.  m5.add_option(" Sorted by ~Relation ",mSORT_RELATION);
  219.  m5.add_option(" Sorted by ~Birthday ",mSORT_BIRTHDAY);
  220.  
  221. ///////////////// Make menu 1 'special' (main menu) ////////////////////
  222.  
  223.  
  224.   m1.type(MENU_HOR);             // Display options horizontally
  225.   m1.hold(MENU_HOLD);             // Make it 'always visible'
  226.   m1.border(BORDER_NONE);         // No border
  227.   m1.width(80);              // Set the width to the full screen
  228.  
  229.  
  230. //////////////////////////// Define the colors //////////////////////////
  231.  
  232.  
  233.   int bor_col;
  234.   int scr_col;
  235.   int opt_col;
  236.   int key_col;
  237.  
  238.  
  239.   if(is_color())
  240.   {
  241.     bor_col=make_color(WHITE,CYAN);
  242.     scr_col=make_color(WHITE,CYAN);
  243.     opt_col=make_color(WHITE,RED);
  244.     key_col=make_color(YELLOW,CYAN);
  245.   }
  246.   else
  247.   {
  248.     bor_col=make_color(LIGHTGRAY,BLACK);
  249.     scr_col=bor_col;
  250.     opt_col=make_color(BLACK,WHITE);
  251.     key_col=make_color(WHITE,BLACK);
  252.   }
  253.  
  254.  
  255. /////////////////////////////// Set the colors of each (Sub) menu /////////
  256.  
  257. //  m1.color(bor_col,make_color(YELLOW,RED),key_col,key_col);
  258.  
  259.   m1.color(bor_col,scr_col,opt_col,key_col);
  260.   m2.color(bor_col,scr_col,opt_col,key_col);
  261.   m3.color(bor_col,scr_col,opt_col,key_col);
  262.   m4.color(bor_col,scr_col,opt_col,key_col);
  263.   m5.color(bor_col,scr_col,opt_col,key_col);
  264.  
  265. ///////////////////////// Set the postions of the menus ////////////////////
  266.  
  267.   m1.coord(1,1);
  268.   m2.relative_pos(TO_CURSOR);
  269.   m2.coord(1,1);
  270.   m3.relative_pos(TO_CURSOR);
  271.   m3.coord(1,1);
  272.   m4.relative_pos(TO_CURSOR);
  273.   m4.coord(1,1);
  274.   m5.relative_pos(TO_CURSOR);
  275.   m5.coord(1,1);
  276.  
  277. ////////////// Create the entire menu by connecting the sub-menus //////////
  278.  
  279.   m1.connect(5,m5);
  280.   m1.connect(6,m2);
  281.   m1.connect(7,m3);
  282.   m1.connect(8,m4);
  283.  
  284.   m1.standby();
  285.  
  286. }
  287.  
  288.  
  289. /////////////////////////////////////////////////////////////////////////
  290.  
  291. void main(int argc,char *argv[])
  292. {
  293.  
  294.    if(argc>=2 && !strcmp(argv[1],"/?"))
  295.    {
  296.  
  297.       printf("\n  * * * * * * * An address database.  * * * * * * *    ");
  298.       printf("\n\n\n CSADDRESS version 1.0.c ");
  299.       printf("\n\n Compiled at:  %s, %s ",__DATE__,__TIME__);
  300.       printf("\n\ Copyright (c) Combis ");
  301.       printf("\n\n USAGE:  csadd [/reindex] [/create] [/?] ");
  302.       printf("\n  /reindex  To rebuild the indexes. ");
  303.       printf("\n  /create   To create a new database. ");
  304.       printf("\n  /?        To display this screen.   ");
  305.       printf("\n\n");
  306.       return;
  307.    }
  308.  
  309.    char inp[50]="";
  310.  
  311.    NAM    nam;
  312.  
  313.  
  314. ////////////////// Create a new database ////////////////////////////////
  315.  
  316.  
  317.    if(argc==2 && !strcmp(argv[1],"/create"))  nam.define();
  318.  
  319.  
  320. ////////////////// Reindex when started with '/reindex' /////////////////
  321.    if(argc==2 && !strcmp(argv[1],"/reindex"))
  322.    {
  323.      nam.open();
  324.      nam.reindex();
  325.      nam.close();
  326.      exit(0);
  327.    }
  328.  
  329.    int    choice=-1;
  330.    int    leng;
  331.  
  332.    clrscr();
  333.  
  334.  
  335. ////////////////// Open database and indexes. ///////////////////////////
  336.  
  337.    nam.open();
  338.  
  339.  
  340. ////////////////// Setup background /////////////////////////////////////
  341.    WINDOW bckgrnd;
  342.    bckgrnd.border(BORDER_SINGLE);
  343.    bckgrnd.set_dim(2,1,23,80);
  344.    bckgrnd.activate();
  345.  
  346.  
  347. ////////////////// Create input window. /////////////////////////////////
  348.    WINDOW input;
  349.    input.border(BORDER_SINGLE);
  350.    input.set_dim(21,4,3,73);
  351.    input.activate();
  352.  
  353.  
  354. ////////////////// Create menus /////////////////////////////////////////
  355.    nam.def_menu();
  356.  
  357.  
  358.    nam.order(NAME_INDEX);
  359.    nam.top();
  360.  
  361.  
  362. ////////////////// Create & display PANEL. //////////////////////////////
  363.    nam.create();
  364.  
  365.  
  366. ////////////////// Main processing loop. ////////////////////////////////
  367.     do
  368.     {
  369.       if(choice==-1)
  370.       {
  371.     nam.display();
  372.     input.activate();
  373.     gotoxy(5,1);
  374.     cprintf("Search string: %s",inp); clreol();
  375.     gotoxy(50,1);
  376.     cprintf("Record %ld/%ld",nam.curr_rec(),nam.numrec());
  377.     clreol();
  378.     gotoxy(69,1);
  379.     if(nam.is_delet()) putch('D');
  380.     else           putch(' ');
  381.     gotoxy(20+strlen(inp),1);
  382.     choice=cskey();
  383.       }
  384.  
  385.       switch(choice)
  386.       {
  387.      case mUNSORT:
  388.              nam.order(UNSORTED);
  389.              nam.top();
  390.              choice=-3;
  391.              break;
  392.      case mSORT_NAME:
  393.              nam.order(NAME_INDEX);
  394.              choice=-2;
  395.              break;
  396.      case mSORT_CITY:
  397.              nam.order(CITY_INDEX);
  398.              choice=-2;
  399.              break;
  400.      case mSORT_RELATION:
  401.              nam.order(RELATION_INDEX);
  402.              choice=-2;
  403.              break;
  404.      case mSORT_BIRTHDAY:
  405.              nam.order(BIRTH_INDEX);
  406.              choice=-2;
  407.              break;
  408.      case 601:
  409.      case 602:
  410.      case 603:
  411.      case 604:
  412.      case 605:
  413.              message_window("Sorry, option not implemented.\n\rHit any key...");
  414.              waitkb(3500);
  415.              message_window_clear();
  416.              choice=-1;
  417.              break;
  418.      case mCREATE_DB:
  419.              message_window("Deleting entire database! \n\rARE YOU SURE (y/n).");
  420.              if(cskey()=='y')
  421.              {
  422.             message_window("Creating new database.");
  423.             nam.close();
  424.             nam.define();
  425.             nam.open();
  426.              }
  427.              message_window_clear();
  428.              choice=-3;
  429.              break;
  430.      case mREINDEX:
  431.              message_window("Indexing... A moment please.");
  432.              nam.reindex();
  433.              message_window_clear();
  434.              choice=-2;
  435.              break;
  436.      case mEXPORT_ASCII:
  437.              message_window("Exporting to 'backup.txt'.");
  438.              nam.export("backup.txt");
  439.              message_window_clear();
  440.              choice=-1;
  441.              break;
  442.      case mEXPORT_DBASE:
  443.              message_window("Exporting to 'DBASE.dbf'.");
  444.              nam.to_DBASE("DBASE.dbf");
  445.              message_window_clear();
  446.              choice=-1;
  447.              break;
  448.      case mIMPORT_ASCII:
  449.              message_window("Importing from 'backup.txt'.");
  450.              nam.import("backup.txt");
  451.              message_window_clear();
  452.              choice=-3;
  453.              break;
  454.      case F1:
  455.      case ALT_H:
  456.              {
  457.             WINDOW help;
  458.             help.set_dim(-1,-1,14,50);
  459.             help.head(" Help Screen ");
  460.             help.activate();
  461.  
  462.             cprintf("\n                 Main Keys ");
  463.             cprintf("\n\r");
  464.             cprintf("\n\r    Edit:     ALT-E    ");
  465.             cprintf("\n\r    End Edit: CTRL-END ");
  466.             cprintf("\n\r");
  467.             cprintf("\n\r    Insert:   ALT-I ");
  468.             cprintf("\n\r    Delete:   ALT-D ");
  469.             cprintf("\n\r    Exit:     ALT-X ");
  470.             cprintf("\n\r    Menu:     F10   ");
  471.             cprintf("\n\r    Clear search key: ESC");
  472.             cprintf("\n\n\r              Hit any key");
  473.             cskey();
  474.  
  475.              }
  476.              choice=-1;
  477.              break;
  478.      case ALT_D:
  479.              if(nam.is_delet()) nam.undelet();
  480.              else        nam.delet();
  481.              choice=-1;
  482.              break;
  483.      case ALT_E:
  484.              nam.edit();
  485.              choice=-3;
  486.              break;
  487.      case ALT_I:
  488.              nam.insert();
  489.              choice=-3;
  490.              break;
  491.      case ALT_P:
  492.              message_window("Packing... A moment please.. ");
  493.              nam.pack();
  494.              message_window_clear();
  495.              nam.top();
  496.              choice=-3;
  497.              break;
  498.      case ALT_X:
  499.              break;
  500.      case ALT_O:
  501.      case ALT_S:
  502.      case ALT_T:
  503.      case ALT_U:
  504.              choice=nam.use_menu(choice);
  505.              break;
  506.      case F10:
  507.              choice=nam.use_menu();
  508.              break;
  509.      case CURSOR_UP:
  510.              nam.skip(-1);
  511.              choice=-3;
  512.              break;
  513.      case CURSOR_DOWN:
  514.              nam.skip(1);
  515.              choice=-3;
  516.              break;
  517.      case PAGE_DOWN:
  518.              nam.skip(10);
  519.              choice=-3;
  520.              break;
  521.      case PAGE_UP:
  522.              nam.skip(-10);
  523.              choice=-3;
  524.              break;
  525.      case HOME:
  526.              nam.top();
  527.              choice=-3;
  528.              break;
  529.      case END:
  530.              nam.bottom();
  531.              choice=-3;
  532.              break;
  533.      case BACKSPACE:
  534.              if(strlen(inp))
  535.              {
  536.                inp[strlen(inp)-1]=0;
  537.                choice=-2;
  538.              }
  539.              else choice=-1;
  540.              break;
  541.      case ESC:
  542.              inp[0]=0;
  543.      case -2:
  544.              nam.search(inp);
  545.      case -3:
  546.              // Adjust panel to new record.
  547.              nam.recreate();
  548.              choice=-1;
  549.              break;
  550.      default:
  551.              if(isprint(choice))
  552.              {
  553.             // Modify search string.
  554.             leng=strlen(inp);
  555.             inp[leng]=choice;
  556.             inp[leng+1]=0;
  557.             choice=-2;
  558.              }
  559.              else choice=-1;
  560.              break;
  561.       }
  562.  
  563.     } while(choice!=ALT_X);
  564.  
  565.  
  566. ////////////////// Remove all windows. //////////////////////////////////
  567.     win_remove_all();
  568.  
  569.  
  570. ////////////////// Close database. //////////////////////////////////////
  571.     nam.close();
  572.  
  573.  
  574. }
  575.  
  576.